/*!
// Testweb v0.3.0
// http://or-change.cn
// Copyright 2014 OrChange Inc. All rights reserved.
// Licensed under the GPL License.
*/
define(function (require, exports, module) {
	'use strict';
	var helper = require("model/helper"),
		jQuery = require("lib/jquery/jquery");
	exports.execute = function (me, $, load) {
		return function () {
			var step_tpl = $(this);

			helper.moveToCenter(me);
			helper.initWindowTemplate(me);

			$("#catch").click(function () {
				step_tpl.hide();
				load.edit.catchObject(function (object) {
					$("input#object").val(object);
					step_tpl.show();
					$("#path-result").html(jQuery(object, window[0].document).length);
				});
			});

			$("input#object").keyup(function () {
				var path, amount = 0;
				path = $(this).val();
				$("#path-result").html(jQuery(path, window[0].document).length);
			});

			$("[name=type],[name=action],select[name=param_value],[name=param_type]").change(function () {
				var i, len, form = $("form").serializeArray(),
					data = {
						form: {}
					};
				len = form.length;
				for (i = 0; i < len; i += 1) {
					data.form[form[i].name] = form[i].value;
				}
				load.tpm.refresh(me, data);
			});

			$("#append").click(function () {
				var key, action = load.tpm.getTemplate(me).getData("action"),
					form = $("form").serializeObject();
				if (form.action === "val") {
					form.param = {
						type: form.param_type,
						value: form.param_value
					};
					delete form.param_type;
					delete form.param_value;
				} else if (form.action === "scroll") {
					form.param = {
						position_top: form.position_top,
						position_left: form.position_left
					};
					delete form.position_left;
					delete form.position_top;
				}

				// ppm 追加一个步骤
				if (action.method === "new") {
					load.ppm.appendTo(load.edit.getStep(form));
				} else if (action.method === "addUp") {
					load.ppm.appendTo(load.edit.getStep(form), parseInt(action.index, 0));
				} else if (action.method === "addDown") {
					load.ppm.appendTo(load.edit.getStep(form), parseInt(action.index, 0) + 1);
				}
				// 刷新ui
				load.tpm.refresh("ui", load.ppm.getProgramList()).close(me);
			});

			$("#modify").click(function () {
				var key, form = $("form").serializeObject();
				if (form.param_type !== undefined) {
					form.param = {
						type: form.param_type,
						value: form.param_value
					};
					delete form.param_type;
					delete form.param_value;
				} else if (form.action === "scroll") {
					form.param = {
						position_top: form.position_top,
						position_left: form.position_left
					}
					delete form.position_left;
					delete form.position_top;
				}

				load.ppm.replaceStep(form.step_index, load.edit.getStep(form));
				load.tpm.refresh("ui", load.ppm.getProgramList()).close(me);
			});

		};
	};
});